/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.general.economic;

import gov.fema.hazus.bdc.client.common.ui.AbstractForm;
import gov.fema.hazus.bdc.client.common.ui.BuildingUpdate;
import gov.fema.hazus.bdc.client.common.ui.TextBoxDoubleValueBlurHandler;
import gov.fema.hazus.bdc.client.common.ui.TextBoxIntegerValueBlurHandler;
import gov.fema.hazus.bdc.client.general.GeneralConstants;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.Economy;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class EconomicForm extends AbstractForm implements BuildingUpdate {

	// labels
	private Label buildingValue;
	private Label valuationType;
	private Label valuationYear;
	private Label contentsValue;
	private Label inventoryValue;
	private Label rentalIncomePerMonth;
	private Label businessIncomePerDay;
	private Label wagesPerDay;
	
	// form fields
	private TextBox formBuildingValue = new TextBox();
	private TextBox formValuationType = new TextBox();
	private TextBox formValuationYear = new TextBox();
	private TextBox formContentsValue = new TextBox();
	private TextBox formInventoryValue = new TextBox();
	private TextBox formRentalIncomePerMonth = new TextBox();
	private TextBox formBusinessIncomePerDay = new TextBox();
	private TextBox formWagesPerDay = new TextBox();
	
	// ui components
	private DockPanel docker;
	private Grid table = new Grid(8,2);	
	
	// entities
	Economy economy;
	
	// listeners
	EconomicTextBoxKeyListener economicTextBoxKeyListener;
	private TextBoxDoubleValueBlurHandler textBoxDoubleValueBlurHandler;
	private TextBoxIntegerValueBlurHandler textBoxIntegerValueBlurHandler;
	
	private GeneralConstants constants;
	
	public EconomicForm(Building building) {
		
		// do parent
		super(building);
		
		// set up widgets
		docker = new DockPanel();
		
		// set up tab title
		constants = (GeneralConstants)GWT.create(GeneralConstants.class);
		tabTitle = constants.generalEconomicTabTitle();
		
		buildingValue = new Label(constants.generalEconomicBuildingValue());
		valuationType = new Label(constants.generalEconomicValuationType());
		valuationYear = new Label(constants.generalEconomicValuationYear());
		contentsValue = new Label(constants.generalEconomicContentsValue());
		inventoryValue = new Label(constants.generalEconomicInventoryValue());
		rentalIncomePerMonth = new Label(constants.generalEconomicRentalIncomePerMonth());
		businessIncomePerDay = new Label(constants.generalEconomicBusinessIncomePerDay());
		wagesPerDay = new Label(constants.generalEconomicWagesPerDay());
		
		// add labels to form
		table.setWidget(0, 0, buildingValue);
		table.setWidget(1, 0, valuationType);
		table.setWidget(2, 0, valuationYear);
		table.setWidget(3, 0, contentsValue);
		table.setWidget(4, 0, inventoryValue);
		table.setWidget(5, 0, rentalIncomePerMonth);
		table.setWidget(6, 0, businessIncomePerDay);
		table.setWidget(7, 0, wagesPerDay);
		
		
		// get entity
		economy = building.getEconomy();
		
		// set up listeners
		economicTextBoxKeyListener = new EconomicTextBoxKeyListener(economy);
		textBoxDoubleValueBlurHandler = new TextBoxDoubleValueBlurHandler();
		textBoxIntegerValueBlurHandler = new TextBoxIntegerValueBlurHandler();
		
		// load listeners
		loadListeners();
		
		// load forms
		loadForms();
	
		// add fields to table
		table.setWidget(0, 1, formBuildingValue);
		table.setWidget(1, 1, formValuationType);
		table.setWidget(2, 1, formValuationYear);
		table.setWidget(3, 1, formContentsValue);
		table.setWidget(4, 1, formInventoryValue);
		table.setWidget(5, 1, formRentalIncomePerMonth);
		table.setWidget(6, 1, formBusinessIncomePerDay);
		table.setWidget(7, 1, formWagesPerDay);
		
		// add table to docker
		docker.add(table, DockPanel.CENTER);
		
		initWidget(docker);		
				
	}

	private void loadForms() {
		
		if( economy != null )
		{
			// set up form fields
			String buildingValue = null;
			if (economy.getBuildingValue() != null) {
				buildingValue = economy.getBuildingValue().toString();
			} 
			formBuildingValue.setText( buildingValue );
			formBuildingValue.setName( Economy.BUILDINGVALUE );
			
			
			formValuationType.setText( economy.getValuationType() );
			formValuationType.setName( Economy.VALUATIONTYPE );
			
			
			String valuationYear = null;
			if (economy.getValuationYear() != null) {
				valuationYear = economy.getValuationYear().toString();
			}
			formValuationYear.setText( valuationYear );
			formValuationYear.setName( Economy.VALUATIONYEAR );
			
			
			String contentsValue = null;
			if (economy.getContentsValue() != null) {
				contentsValue = economy.getContentsValue().toString();
			} 
			formContentsValue.setText( contentsValue );
			formContentsValue.setName( Economy.CONTENTSVALUE );
			
			
			String inventoryValue = null;
			if (economy.getInventoryValue() != null) {
				inventoryValue = economy.getInventoryValue().toString();
			} 
			formInventoryValue.setText( inventoryValue );
			formInventoryValue.setName( Economy.INVENTORYVALUE );
			
			
			String rentalIncomePerMonth = null;
			if (economy.getRentalIncomePerMonth() != null) {
				rentalIncomePerMonth = economy.getRentalIncomePerMonth().toString();
			} 
			formRentalIncomePerMonth.setText( rentalIncomePerMonth );
			formRentalIncomePerMonth.setName( Economy.RENTALINCOME );
			
			
			String businessIncomePerDay = null;
			if (economy.getBusinessIncomePerDay() != null) {
				businessIncomePerDay = economy.getBusinessIncomePerDay().toString();
			} 
			formBusinessIncomePerDay.setText( businessIncomePerDay );
			formBusinessIncomePerDay.setName( Economy.BUSINESSINCOME );
			
			
			String wagesPerDay = null;
			if (economy.getWagesPerDay() != null) {
				wagesPerDay = economy.getWagesPerDay().toString();
			} 
			formWagesPerDay.setText( wagesPerDay );
			formWagesPerDay.setName( Economy.WAGESPERDAY );
		}

		if( building != null )
		{
	        if( building.getMode().equalsIgnoreCase(Building.READ_ONLY))
	        {
	            formBuildingValue.setEnabled(false);
	            formValuationType.setEnabled(false);
	            formValuationYear.setEnabled(false);
	            formContentsValue.setEnabled(false);
	            formInventoryValue.setEnabled(false);
	            formRentalIncomePerMonth.setEnabled(false);
	            formBusinessIncomePerDay.setEnabled(false);
	            formWagesPerDay.setEnabled(false);            
	        } else
	        {
	            formBuildingValue.setEnabled(true);
	            formValuationType.setEnabled(true);
	            formValuationYear.setEnabled(true);
	            formContentsValue.setEnabled(true);
	            formInventoryValue.setEnabled(true);
	            formRentalIncomePerMonth.setEnabled(true);
	            formBusinessIncomePerDay.setEnabled(true);
	            formWagesPerDay.setEnabled(true);   
	        }
		}
	
	}

	private void loadListeners() {
		
		formBuildingValue.addFocusListener( economicTextBoxKeyListener );
		formBuildingValue.addBlurHandler(textBoxDoubleValueBlurHandler);
		
		formValuationType.addFocusListener( economicTextBoxKeyListener );
		
		formValuationYear.addFocusListener( economicTextBoxKeyListener );
		formValuationYear.addBlurHandler(textBoxIntegerValueBlurHandler);
		
		formContentsValue.addFocusListener( economicTextBoxKeyListener );
		formContentsValue.addBlurHandler(textBoxDoubleValueBlurHandler);
		
		formInventoryValue.addFocusListener( economicTextBoxKeyListener );
		formInventoryValue.addBlurHandler(textBoxDoubleValueBlurHandler);
		
		formRentalIncomePerMonth.addFocusListener( economicTextBoxKeyListener );
		formRentalIncomePerMonth.addBlurHandler(textBoxDoubleValueBlurHandler);
		
		formBusinessIncomePerDay.addFocusListener( economicTextBoxKeyListener );
		formBusinessIncomePerDay.addBlurHandler(textBoxDoubleValueBlurHandler);
		
		formWagesPerDay.addFocusListener( economicTextBoxKeyListener );
		formWagesPerDay.addBlurHandler(textBoxDoubleValueBlurHandler);
		
	}

	public void updateBuilding(Building building) {
		
		// entities
		setBuilding(building);
		this.economy = building.getEconomy();
		
		// listeners
		economicTextBoxKeyListener.updateEconomy(economy);
		
		// reload forms
		loadForms();		
	}
	
}
